package com.home_school_platform.service.impl.login;

import com.home_school_platform.dao.TeamDao;
import com.home_school_platform.entity.pojo.Team;
import com.home_school_platform.result.Result;
import com.home_school_platform.service.login.TeacherLoginService;
import com.home_school_platform.util.JwtUtils;
import com.home_school_platform.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 教师登录实现类
 * @authoor Bilysen
 * @date 2021/3/14 - 19:53
 **/
@Service
public class TeacherLoginServiceImpl implements TeacherLoginService {
    @Autowired
    TeamDao teamDao;

    @Override
    public Result checkAccountLogin(String account, String password) {
        //标志是否验证成功
        boolean flag = false;

        //map封装发给前端的信息
        HashMap<String, Object> map = new HashMap<>();

        Result result = new Result();

        Team teamAccount = teamDao.getTeamByAccount(account);

        if(teamAccount!=null && "启用".equals(teamAccount.getState())){
            //判断密码是否正确，（数据库中已经使用md5加密）
            if(MD5Utils.textToMd5(password).equals(teamAccount.getPassword())){
                flag = true;
                map.put("flag",flag); //返回true,表示验证成功
                map.put("token",JwtUtils.getToken(teamAccount.getAccount())); //返回给前端以班级账号生成的jwt
                result.setData(map);
                result.setCode(Result.OK); //返回200给前端
                result.setMassage("登录成功");
            }else{
                map.put("flag",flag);
                result.setData(map);
                result.setCode(Result.NULL); // 返回400
                result.setMassage("密码错误"); //放回错误提示信息
            }
        }else{
            map.put("flag",flag);
            result.setData(map);
            result.setCode(Result.NULL); // 返回400
            result.setMassage("账号错误"); //放回错误提示信息
        }
        return result;
    }

}
